﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Twodi.Arguments;

namespace Twodi.Input.Events
{
    /// <summary>
    /// Tipos de eventos do teclado.
    /// </summary>
    public enum KeyboardEventsType
    {
        KeyDown,
        KeyUp,
        KeyPressed
    }

    /// <summary>
    /// Evento de teclado.
    /// </summary>
    public class KeyboardEvent : AbstractInputEvent<KeyboardEventsType, KeyboardInput>
    {
        protected Keys[] keys;

        public KeyboardEvent(KeyboardEventsType type, params Keys[] keys)
            : base(type)
        {
            this.Keys = keys;
        }

        public KeyboardEvent(KeyboardEventsType type, InputEventHandler<InputEventArgs<KeyboardInput>> method, params Keys[] keys)
            : base(type, method)
        {
            this.Keys = keys;
        }

        private delegate bool Checker(Keys key);
        public override bool Handle(KeyboardInput input)
        {
            bool result = true;
            int index = 0;
            Checker method = null;

            switch (Type)
            {
                case KeyboardEventsType.KeyDown:
                    method = input.IsKeyDown;
                    break;
                case KeyboardEventsType.KeyUp:
                    method = input.IsKeyUp;
                    break;
                case KeyboardEventsType.KeyPressed:
                    method = input.IsKeyPressed;
                    break;
            }

            while (result && method != null)
            {
                if (index == keys.Length) break;
                result = method(keys[index]);
                index++;
            }

            if (result) return onEvents(new InputEventArgs<KeyboardInput>(input));
            return true;
        }

        public Keys[] Keys
        {
            get { return this.keys; }
            set
            {
                this.keys = value;
            }
        }
    }
}
